home *** CD-ROM | disk | FTP | other *** search
/ Resource Library: Multimedia / Resource Library: Multimedia.iso / hypercrd / xcmds / rtf-redr.hqx / lists.c < prev    next >
Text File  |  1992-12-19  |  3KB  |  199 lines

  1. /*
  2.  * This software is copyright 1992 by Robert Morris.
  3.  * You may freely redistribute this software as shareware
  4.  * if you do so in the same form as you got it. If you find
  5.  * this software useful, please send $12 to:
  6.  *   Robert Morris
  7.  *   P.O. Box 1044
  8.  *   Harvard Square Station
  9.  *   Cambridge, MA 02238
  10.  *   ecognome@aol.com
  11.  * If you incorporate any of this software in any kind of
  12.  * commercial product, please send $2 per copy distributed
  13.  * to the above address.
  14.  */
  15.  
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include "lists.h"
  19.  
  20. long
  21. NewList(h, lp)
  22. char **h;
  23. struct list *lp;
  24. {
  25.     if(h){
  26.         lp->h = h;
  27.         lp->size = GetHandleSize(h);
  28.         lp->ptr = 0;
  29.     } else {
  30.         lp->size = 256;
  31.         lp->h = NewHandle(lp->size);
  32.         if(lp->h == 0)
  33.             return(-1);
  34.         lp->ptr = 0;
  35.         (*(lp->h))[0] = '\0';
  36.     }
  37.     return(0);
  38. }
  39.  
  40. void
  41. FreeList(lp)
  42. struct list *lp;
  43. {
  44.     if(lp->h)
  45.         DisposHandle(lp->h);
  46.     lp->h = 0;
  47. }
  48.  
  49. /*
  50.  * get rid of excess storage at the end of the handle.
  51.  */
  52. long
  53. TrimList(lp)
  54. struct list *lp;
  55. {
  56.     if(lp->h == 0)
  57.         return(-1);
  58.     SetHandleSize(lp->h, lp->ptr + 1);
  59.     lp->size = GetHandleSize(lp->h);
  60.     if(lp->size > 0)
  61.         (*(lp->h))[lp->size - 1] = '\0';
  62.     return(lp->ptr);
  63. }
  64.  
  65. long
  66. PeekListLine(lp, buf)
  67. register struct list *lp;
  68. register char *buf;
  69. {
  70.     /* THINK C supplies 5 data and 3 pointer register vars */
  71.     register int c;
  72.     register long i;
  73.     register char *ptr;
  74.     
  75.     buf[0] = '\0';
  76.     if(lp->h == 0 || *(ptr = (*(lp->h))+lp->ptr) == '\0')
  77.         return(-1);
  78.         
  79.     i = 0;
  80.     while((c = *(ptr++)) != '\0' && c != '\r'){
  81.         if(i < 255)
  82.             buf[i++] = c;
  83.     }
  84.     if(c == '\0')
  85.         --ptr;
  86.     buf[i] = '\0';
  87.     return(ptr - (*(lp->h) + lp->ptr));
  88. }
  89.  
  90. /*
  91.  * skip back a line.
  92.  * assumes we're at the start of a line, just after the \r.
  93.  */
  94. void
  95. RewindListLine(lp)
  96. struct list *lp;
  97. {
  98.     if(lp->h == 0)
  99.         return;
  100.     
  101.     lp->ptr -= 2;
  102.     while(lp->ptr >= 0 && (*(lp->h))[lp->ptr] != '\r')
  103.         lp->ptr -= 1;
  104.     if(lp->ptr < 0)
  105.         lp->ptr = 0;
  106.     else
  107.         lp->ptr += 1;
  108. }
  109.  
  110. /*
  111.  * reads the next line from a list into buf. returns -1 on EOF.
  112.  */
  113. long
  114. ReadListLine(lp, buf)
  115. struct list *lp;
  116. char buf[];
  117. {
  118.     long n;
  119.     
  120.     n = PeekListLine(lp, buf);
  121.     if(n < 0)
  122.         return(-1);
  123.     lp->ptr += n;
  124.     return(strlen(buf));
  125. }
  126.  
  127. /*
  128.  * appends some text to a list. returns -1 on error (out of memory).
  129.  */
  130. long
  131. AppendList(lp, buf)
  132. struct list *lp;
  133. char buf[];
  134. {
  135.     long len;
  136.     
  137.     if(lp->h == 0)
  138.         return(-1);
  139.         
  140.     len = strlen(buf);
  141.     
  142.     if(lp->ptr + len + 1 > lp->size){
  143.         if(lp->size > 4096)
  144.             lp->size += (len + 4096);
  145.         else
  146.             lp->size += (len + lp->size);
  147.         SetHandleSize(lp->h, lp->size);
  148.         if(MemError() != noErr){
  149.             DisposHandle(lp->h);
  150.             lp->h = 0;
  151.             lp->size = -1;
  152.             lp->ptr = 0;
  153.             return(-1);
  154.         }
  155.     }
  156.     
  157.     strcpy((*(lp->h)) + lp->ptr, buf);
  158.     lp->ptr += len;
  159.     return(len);
  160. }
  161.  
  162. int
  163. _ALC(lp, c)
  164. struct list *lp;
  165. {
  166.     char buf[2];
  167.     
  168.     buf[0] = c;
  169.     buf[1] = '\0';
  170.     AppendList(lp, buf);
  171. }
  172.                                 
  173. /*
  174.  * get the itm'th sep-separated item from in[].
  175.  * 1 origin.
  176.  * out[] should hold 256 chars.
  177.  */
  178. char *
  179. item(itm, in, out, sep)
  180. register int itm, sep;
  181. register char *in, *out;
  182. {
  183.     register int i, j;
  184.     
  185.     for(i = 0; itm > 1 && in[i]; i++){
  186.         if(in[i] == sep)
  187.             --itm;
  188.     }
  189.     
  190.     j = 0;
  191.     if(itm == 1){
  192.         while(in[i] && in[i] != sep && j < 255){
  193.             out[j++] = in[i++];
  194.         }
  195.     }
  196.     out[j] = '\0';
  197.     return(out);
  198. }
  199.